package gLib::gConf;

## ----------------------------------------------------
## Title :			Configuration Manager Class
## Description :	Provide generic functions to manage configuration value from file
## Date :			2013-07-13
## Version :		0.1
## Author :			@TiTom
## ----------------------------------------------------

## ----------------------------------------------------
## Todo List
## ----------------------------------------------------
## - Must do Everything
## ----------------------------------------------------

=head1 NAME

gConf Module

=head1 DESCRIPTION

Provide some simple functions to parse perl configuration file. It is based on a CPAN module Config::IniFiles and implement easy to use functions as an abstract layer.

=head1 Author

@TiTom73 (dev@inetsix.net)

=head1 SYNOPSYS

Usage is :
	
	use gLib::gConf;
	
	my $cfg = new gLib::gConf("/path/to/my/config.file");
	my $entry = $cfg->value("CATEGORIE","KEY")

	Configuration file is construct like below :

=head1 DEPENDENCIES

This module is based on :
- strict
- warnings
- POSIX
- Config::IniFiles : http://search.cpan.org/~shlomif/Config-IniFiles-2.82/lib/Config/IniFiles.pm
=cut
########################################################
#	Requirements
########################################################
use strict;
use warnings;
use POSIX qw/strftime/;
use Config::IniFiles;

=head1 VERSION

Verison = 0.1
=cut 
########################################################
#	Code Version
########################################################
our $VERSION = 0.1;

########################################################
#	Constants
########################################################

use constant TRUE=> 1;
use constant FALSE=> 0;

########################################################
#	FUNCTIONS
########################################################
=head1 METHODS
=cut
## ----------------------------------------------------
## Package to provide some usefull functions
## Class Constructor
sub new {
	my ($class,$conf) = @_;

	my $self = {
		_DEBUG		=> TRUE,
		_CONF		=> $conf,
		_CFG		=> undef,
	};
	bless	$self,$class;
	return	$self;
}
## ----------------------------------------------------
## Function to manage CONFIG File Value
=head2 debug

Set DEBUG value to enable or disable it.
If there is no value, function return STATUS

=cut
sub debug {
	my ( $self, $debug ) = @_;
	$self->{_DEBUG} = $debug if defined($debug);
	return $self->{_DEBUG};
}

## ----------------------------------------------------
## Function to manage FILE Value
=head2 file

Set FILE value to enable or disable it.
If there is no value, function return config file 

=cut
sub file {
	my ( $self, $entry ) = @_;
	$self->{_CONF} = $entry if defined($entry);
	return $self->{_CONF};
}

## ----------------------------------------------------
## Function to search value in Configuration File.
=head2 file

Search KEY in the specified section and return its value if it exists
If this value is not defined, return FALSE (0)

=cut
sub value {
        ## Get position and key to search
        my ($self,$section,$id)=@_;
		my $conf = $self->{_CONF};
		my $cfg = Config::IniFiles->new(-file =>$conf);
        ## Check if value exist
        ## If yes, then return it
        if( $cfg->exists($section,$id)) { return $cfg->val($section,$id) }
        else { return FALSE }
}

1;
